package commonClasses;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class AbstractSearch {
	
	public static void main(String []args) throws IOException, SAXException, ParserConfigurationException{

		FileReader f = new FileReader(
				"J:\\BC3_IMT_Training\\annotations\\IMT_training_pmids.txt");
		BufferedReader fb = new BufferedReader(f);
		String name = fb.readLine();
		PrintStream ps=new PrintStream("j:\\BC3_IMT_Training\\absSearch.txt");
		while (name != null) {
			

				DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

				DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
	            Document doc = docBuilder.parse (new File("J:\\BC3_IMT_Training\\articles\\pubmed_xml\\"+name+".xml"));
	        	FileReader dfr = new FileReader("J:\\BC3_IMT_Training\\PSIMI.txt");
				BufferedReader dfbr = new BufferedReader(dfr);
				String str = dfbr.readLine();

				while (str != null) {
					String dict = str.substring(8);
					String did = str.substring(0, 7);
					//System.out.println(dict);
	           
	            NodeList nTitle=doc.getElementsByTagName("ArticleTitle");
	            Element t=(Element) nTitle.item(0);
	           // System.out.println(t.getTextContent());
	            
	            String title=t.getTextContent();
	            
	            
	    		NodeList nList = doc.getElementsByTagName("AbstractText");
	    		Element e=(Element) nList.item(0);
	    		//System.out.println(e.getTextContent());
	    		String abs=e.getTextContent();
	    		
	    		
	    		NodeList nMesh=doc.getElementsByTagName("DescriptorName");
	    		int l=nMesh.getLength();
	    		int c =0;
	    		while(c<l){
	    			Element m=(Element) nMesh.item(c);
	    			c++;
	    			String mesh=m.getTextContent();
	    			if(mesh.indexOf(dict)!=-1)
	    				ps.println(name + "\t" + did+"\tMesh Heading");
	    		}
	    		
	    		
	    		if (title.indexOf(dict) != -1) {
					ps.println(name + "\t" + did+"\tTitle");
					
				}
	    		if(abs.indexOf(dict)!=-1){
	    			ps.println(name+"\t"+did+"\tAbstract");
	    		}
	    		
	    		str=dfbr.readLine();
				}
				System.out.println(name);
	    		

			name = fb.readLine();
		}
	
		
	}

}
